﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.IO;
using System.Windows.Media.Imaging;
using Microsoft.Phone.Info;
using System.Windows.Data;

namespace OmnipotentFlashlight
{
    public class Common
    {
        public static IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

        //设置
        public static string GetSetting(string key)
        {
            if (settings.Contains(key))
                return settings[key].ToString();
            else
                return null;
        }

        //获取设置
        public static void SetSetting(string key, string value)
        {
            if (settings.Contains(key))
                settings[key] = value;
            else
                settings.Add(key, value);
            settings.Save();
        }

        //十六进制颜色转Color对象
        public static Color HtmlToColor(string htmlColr)
        {
            int baseIndex = 1;
            byte a, r, g, b;
            a = r = g = b = 255;
            if (htmlColr.Length == 9)
            {
                a = Convert.ToByte(htmlColr.Substring(baseIndex, 2), 16);
                baseIndex += 2;
            }
            r = Convert.ToByte(htmlColr.Substring(baseIndex, 2), 16);
            g = Convert.ToByte(htmlColr.Substring(baseIndex += 2, 2), 16);
            b = Convert.ToByte(htmlColr.Substring(baseIndex += 2, 2), 16);
            return Color.FromArgb(a, r, g, b);
        }

        internal static bool IsHtcMozart
        {
            get
            {
                string str = DeviceStatus.DeviceName.ToLower();
                if (!DeviceStatus.DeviceManufacturer.ToLower().Contains("htc") || ((!str.Contains("mozart") && !str.Contains("t8697")) && (!str.Contains("t8698") && !str.Contains("t8699"))))
                {
                    return false;
                }
                return true;
            }
        }

        public static childItem FindVisualChild<childItem>(DependencyObject obj)
        where childItem : DependencyObject
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                if (child != null && child is childItem)
                    return (childItem)child;
                else
                {
                    childItem childOfChild = FindVisualChild<childItem>(child);
                    if (childOfChild != null)
                        return childOfChild;
                }
            }
            return null;
        }

    }

    public class Stri : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            int stri = System.Convert.ToInt32(value);
            string tag = (string)parameter;
            SolidColorBrush so = new SolidColorBrush(Common.HtmlToColor("#33FFFFFF"));
            if (tag == "1" && stri >= 1)
                so.Color = Common.HtmlToColor("#B2FFFFFF");
            if (tag == "2" && stri >= 2)
                so.Color = Common.HtmlToColor("#B2FFFFFF");
            if (tag == "3" && stri >= 3)
                so.Color = Common.HtmlToColor("#B2FFFFFF");
            if (tag == "4" && stri >= 4)
                so.Color = Common.HtmlToColor("#B2FFFFFF");
            if (tag == "5" && stri >= 5)
                so.Color = Common.HtmlToColor("#B2FFFFFF");
            return so;

        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
